<?php

declare(strict_types=1);

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;
use think\facade\Db;
use think\facade\Session;

class GmEmailexa extends Model
{
    use SoftDelete;
    protected $deleteTime = "delete_time";
    // 获取列表
    public static function getList()
    {
        $where = [];
        $limit = input('get.limit');

        // 如果不是超级管理员，添加限制条件
        if (Session::get('admin.id') == '1' || Session::get('admin.id') == '21' || Session::get('admin.id') == '18' || Session::get('admin.id') == '10' || Session::get('admin.id') == '9') {
        } else {
            $where[] = ['b.id', '=', Session::get('admin.id')];
        }
        //按标题查找
        if ($title = input("title")) {
            $where[] = ["a.title", "like", "%" . $title . "%"];
        }
        //按内容查找
        if ($content = input("content")) {
            $where[] = ["a.content", "like", "%" . $content . "%"];
        }
        //按命令查找
        if ($cmdtxt = input("cmdtxt")) {
            $where[] = ["a.cmdtxt", "like", "%" . $cmdtxt . "%"];
        }
        $exastatus = input("exastatus");
        $newexastatus = ($exastatus == 'aaa' || $exastatus == 'bbb' || $exastatus == 'ccc') ?
            ($exastatus == 'aaa' ? '0' : ($exastatus == 'bbb' ? '1' : '2')) : '0';

        $where[] = ["a.exastatus", "=", $newexastatus];

        //按审核状态 0.未审核 1.已审核 2.未通过查找
        //    if ($exastatus = input("exastatus")) {
        //        $newexastatus='0';
        //        if($exastatus=='aaa') $newexastatus='0';
        //        if($exastatus=='bbb') $newexastatus='1';
        //        if($exastatus=='ccc') $newexastatus='2';

        //        $where[] = ["a.exastatus", "=", $newexastatus];
        //    }
        //按申请时间查找
        $start = input("get.exatime-start");
        $end = input("get.exatime-end");
        if ($start && $end) {
            $where[] = ["a.exatime", "between", [$start, date("Y-m-d", strtotime("$end +1 day"))]];
        }
        //按发送给谁查找
        if ($touser = input("touser")) {
            $where[] = ["a.touser", "like", "%" . $touser . "%"];
        }
        //按申请发送人查找
        if ($exauser = input("exauser")) {
            $where[] = ["a.exauser", "=", $exauser];
        }
        $list = self::table('gm_emailexa')
            ->field([
                'a.id' => 'id', 'a.sendtype' => 'sendtype', 'a.title' => 'title', 'a.content' => 'content', 'a.exastatus' => 'exastatus', 'a.isdelete' => 'isdelete', 'a.cmdtxt' => 'cmdtxt', 'a.exatime' => 'exatime', 'a.touser' => 'touser', 'b.nickname' => 'exauser', 'a.remake' => 'remake', 'a.quls' => 'quls'
            ])
            ->alias('a')
            ->join('admin_admin b', 'a.exauser = b.id')
            ->order('a.id', 'desc')
            ->where($where)->paginate($limit);

        return ['code' => 0, 'data' => $list->items(), 'extend' => ['count' => $list->total(), 'limit' => $limit]];
    }



    public static function sendEmail($cmdtxt)
    {
        Db::execute($cmdtxt);
    }


    public static function sendEmailPass($cmdtxt)
    {
        // 开启事务
        Db::startTrans();
        try {
            // 将拼接的多条SQL语句分割为单独的语句
            $sqlStatements = explode('|', $cmdtxt);
            foreach ($sqlStatements as $sql) {
                if (trim($sql) !== '') {
                    Db::execute($sql);
                }
            }

            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw $e;
        }
    }
}
